home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / progs / java.exe / Java WorkShop / data.3 / JWS / examples / checkers / CheckersGame.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-05-19  |  6.8 KB  |  374 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Event;
  7. import java.awt.Frame;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.Window;
  11.  
  12. public class CheckersGame extends Applet {
  13.    final int CHECKER_NONE;
  14.    final int CHECKER_RED = 1;
  15.    final int CHECKER_BLACK = 2;
  16.    final int CHECKER_RED_KING = 3;
  17.    final int CHECKER_BLACK_KING = 4;
  18.    final int SQUARE_RED;
  19.    final int SQUARE_BLACK = 1;
  20.    final int squaresPerSide = 8;
  21.    final int pixelsPerBoard = 400;
  22.    final int pixelsPerSquare = 50;
  23.    final int MOVE_INVALID;
  24.    final int MOVE_VALID = 1;
  25.    final int MOVE_HANDLED = 2;
  26.    int[][] pieces = new int[8][8];
  27.    int[][] board = new int[8][8];
  28.    Image blackChecker;
  29.    Image redChecker;
  30.    Image blackKingChecker;
  31.    Image redKingChecker;
  32.    int clipX;
  33.    int clipY;
  34.    int clipWidth;
  35.    int clipHeight;
  36.    int moving_piece;
  37.    int moving_piece_xpos;
  38.    int moving_piece_ypos;
  39.    int moving_piece_x;
  40.    int moving_piece_y;
  41.  
  42.    private int sign(int value) {
  43.       if (value > 0) {
  44.          value = 1;
  45.       } else if (value < 0) {
  46.          value = -1;
  47.       }
  48.  
  49.       return value;
  50.    }
  51.  
  52.    private void SetUpdateRegion(int xpos, int ypos) {
  53.       int halfPixelsPerSquare = 25;
  54.       Dimension d = ((Component)this).getSize();
  55.       int x1 = Math.max(0, Math.min(xpos, this.moving_piece_xpos) - halfPixelsPerSquare);
  56.       int y1 = Math.max(0, Math.min(ypos, this.moving_piece_ypos) - halfPixelsPerSquare);
  57.       int x2 = Math.min(d.width - 1, Math.max(xpos, this.moving_piece_xpos) + halfPixelsPerSquare);
  58.       int y2 = Math.min(d.height - 1, Math.max(ypos, this.moving_piece_ypos) + halfPixelsPerSquare);
  59.       this.clipX = x1;
  60.       this.clipY = y1;
  61.       this.clipWidth = x2 - x1;
  62.       this.clipHeight = y2 - y1;
  63.    }
  64.  
  65.    private int validMove(int piece, int oldX, int oldY, int newX, int newY) {
  66.       int dx = newX - oldX;
  67.       int dy = newY - oldY;
  68.       if (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
  69.          if (this.board[newY][newX] == 0) {
  70.             return 0;
  71.          } else if (this.pieces[newY][newX] != 0) {
  72.             return 0;
  73.          } else {
  74.             switch (piece) {
  75.                case 0:
  76.                default:
  77.                   return 0;
  78.                case 1:
  79.                   if (dy < 1) {
  80.                      return 0;
  81.                   }
  82.  
  83.                   if (Math.abs(dx) == 1 && dy == 1) {
  84.                      if (newY == 7) {
  85.                         this.pieces[newY][newX] = 3;
  86.                         return 2;
  87.                      }
  88.  
  89.                      return 1;
  90.                   }
  91.  
  92.                   if (Math.abs(dx) == 2 && dy == 2) {
  93.                      if (this.pieces[oldY + 1][oldX + this.sign(dx)] != 2 && this.pieces[oldY + 1][oldX + this.sign(dx)] != 4) {
  94.                         return 0;
  95.                      }
  96.  
  97.                      this.pieces[oldY + 1][oldX + this.sign(dx)] = 0;
  98.                      if (newY == 7) {
  99.                         this.pieces[newY][newX] = 3;
  100.                         return 2;
  101.                      }
  102.  
  103.                      return 1;
  104.                   }
  105.                   break;
  106.                case 2:
  107.                   if (dy > -1) {
  108.                      return 0;
  109.                   }
  110.  
  111.                   if (Math.abs(dx) == 1 && dy == -1) {
  112.                      if (newY == 0) {
  113.                         this.pieces[newY][newX] = 4;
  114.                         return 2;
  115.                      }
  116.  
  117.                      return 1;
  118.                   }
  119.  
  120.                   if (Math.abs(dx) == 2 && dy == -2) {
  121.                      if (this.pieces[oldY - 1][oldX + this.sign(dx)] != 1 && this.pieces[oldY - 1][oldX + this.sign(dx)] != 3) {
  122.                         return 0;
  123.                      }
  124.  
  125.                      this.pieces[oldY - 1][oldX + this.sign(dx)] = 0;
  126.                      if (newY == 0) {
  127.                         this.pieces[newY][newX] = 4;
  128.                         return 2;
  129.                      }
  130.  
  131.                      return 1;
  132.                   }
  133.                   break;
  134.                case 3:
  135.                   if (Math.abs(dx) == 1 && Math.abs(dy) == 1) {
  136.                      return 1;
  137.                   }
  138.  
  139.                   if (Math.abs(dx) == 2 && Math.abs(dy) == 2) {
  140.                      if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 2) {
  141.                         this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
  142.                         return 1;
  143.                      }
  144.  
  145.                      if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 4) {
  146.                         this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
  147.                         return 1;
  148.                      }
  149.  
  150.                      return 0;
  151.                   }
  152.                   break;
  153.                case 4:
  154.                   if (Math.abs(dx) == 1 && Math.abs(dy) == 1) {
  155.                      return 1;
  156.                   }
  157.  
  158.                   if (Math.abs(dx) == 2 && Math.abs(dy) == 2) {
  159.                      if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 1) {
  160.                         this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
  161.                         return 1;
  162.                      }
  163.  
  164.                      if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 3) {
  165.                         this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
  166.                         return 1;
  167.                      }
  168.  
  169.                      return 0;
  170.                   }
  171.             }
  172.  
  173.             return 0;
  174.          }
  175.       } else {
  176.          return 0;
  177.       }
  178.    }
  179.  
  180.    public void init() {
  181.       int xcolor = 0;
  182.       int ycolor = 0;
  183.       this.blackChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "blackChecker.gif");
  184.       this.redChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "redChecker.gif");
  185.       this.blackKingChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "blackCheckerKing.gif");
  186.       this.redKingChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "redCheckerKing.gif");
  187.  
  188.       for(int x = 0; x < 8; ++x) {
  189.          ycolor = xcolor % 2;
  190.  
  191.          for(int y = 0; y < 8; ++y) {
  192.             this.board[y][x] = ycolor % 2 == 1 ? 1 : 0;
  193.             ++ycolor;
  194.          }
  195.  
  196.          ++xcolor;
  197.       }
  198.  
  199.       for(int var6 = 0; var6 < 8; ++var6) {
  200.          for(int y = 0; y < 8; ++y) {
  201.             this.pieces[y][var6] = 0;
  202.          }
  203.       }
  204.  
  205.       for(int y = 0; y < 3; ++y) {
  206.          for(int var7 = 1 - y % 2; var7 < 8; var7 += 2) {
  207.             this.pieces[y][var7] = 1;
  208.          }
  209.       }
  210.  
  211.       for(int var11 = 5; var11 < 8; ++var11) {
  212.          for(int var8 = 1 - var11 % 2; var8 < 8; var8 += 2) {
  213.             this.pieces[var11][var8] = 2;
  214.          }
  215.       }
  216.  
  217.       ((Applet)this).resize(400, 400);
  218.    }
  219.  
  220.    public void paint(Graphics g) {
  221.       Color redColor = Color.red;
  222.       Color blackColor = Color.black;
  223.       Dimension d = ((Component)this).getSize();
  224.  
  225.       for(int x = 0; x < 8; ++x) {
  226.          for(int y = 0; y < 8; ++y) {
  227.             switch (this.board[y][x]) {
  228.                case 0:
  229.                   g.setColor(redColor);
  230.                   break;
  231.                default:
  232.                   g.setColor(blackColor);
  233.             }
  234.  
  235.             int xpos = x * 50;
  236.             int ypos = y * 50;
  237.             g.fillRect(xpos, ypos, 50, 50);
  238.          }
  239.       }
  240.  
  241.       for(int var13 = 0; var13 < 8; ++var13) {
  242.          for(int y = 0; y < 8; ++y) {
  243.             int xpos = var13 * 50;
  244.             int ypos = y * 50;
  245.             switch (this.pieces[y][var13]) {
  246.                case 0:
  247.                default:
  248.                   break;
  249.                case 1:
  250.                   g.drawImage(this.redChecker, xpos, ypos, this);
  251.                   break;
  252.                case 2:
  253.                   g.drawImage(this.blackChecker, xpos, ypos, this);
  254.                   break;
  255.                case 3:
  256.                   g.drawImage(this.redKingChecker, xpos, ypos, this);
  257.                   break;
  258.                case 4:
  259.                   g.drawImage(this.blackKingChecker, xpos, ypos, this);
  260.             }
  261.          }
  262.       }
  263.  
  264.       int xpos = this.moving_piece_xpos - 25;
  265.       int ypos = this.moving_piece_ypos - 25;
  266.       switch (this.moving_piece) {
  267.          case 0:
  268.          default:
  269.             break;
  270.          case 1:
  271.             g.drawImage(this.redChecker, xpos, ypos, this);
  272.             break;
  273.          case 2:
  274.             g.drawImage(this.blackChecker, xpos, ypos, this);
  275.             break;
  276.          case 3:
  277.             g.drawImage(this.redKingChecker, xpos, ypos, this);
  278.             break;
  279.          case 4:
  280.             g.drawImage(this.blackKingChecker, xpos, ypos, this);
  281.       }
  282.  
  283.       this.clipX = 0;
  284.       this.clipY = 0;
  285.       this.clipWidth = d.width;
  286.       this.clipHeight = d.height;
  287.    }
  288.  
  289.    public void update(Graphics g) {
  290.       g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
  291.       this.paint(g);
  292.    }
  293.  
  294.    public boolean mouseDown(Event evt, int xpos, int ypos) {
  295.       int x = xpos / 50;
  296.       int y = ypos / 50;
  297.       if (x >= 0 && x < 8 && y >= 0 && y < 8) {
  298.          if (this.pieces[y][x] != 0) {
  299.             this.moving_piece = this.pieces[y][x];
  300.             this.pieces[y][x] = 0;
  301.             this.moving_piece_xpos = xpos;
  302.             this.moving_piece_ypos = ypos;
  303.             this.moving_piece_x = x;
  304.             this.moving_piece_y = y;
  305.             ((Component)this).repaint();
  306.          } else {
  307.             this.moving_piece = 0;
  308.          }
  309.       }
  310.  
  311.       return true;
  312.    }
  313.  
  314.    public boolean mouseUp(Event evt, int xpos, int ypos) {
  315.       int x = xpos / 50;
  316.       int y = ypos / 50;
  317.       if (this.moving_piece == 0) {
  318.          return false;
  319.       } else {
  320.          int moveStatus = this.validMove(this.moving_piece, this.moving_piece_x, this.moving_piece_y, x, y);
  321.          switch (moveStatus) {
  322.             case 0:
  323.                this.pieces[this.moving_piece_y][this.moving_piece_x] = this.moving_piece;
  324.                break;
  325.             case 1:
  326.                this.pieces[y][x] = this.moving_piece;
  327.             case 2:
  328.          }
  329.  
  330.          this.moving_piece = 0;
  331.          ((Component)this).repaint();
  332.          return true;
  333.       }
  334.    }
  335.  
  336.    public void mouseExit() {
  337.       if (this.moving_piece != 0) {
  338.          this.pieces[this.moving_piece_y][this.moving_piece_x] = this.moving_piece;
  339.          this.moving_piece = 0;
  340.          ((Component)this).repaint();
  341.       }
  342.    }
  343.  
  344.    public boolean mouseDrag(Event evt, int xpos, int ypos) {
  345.       if (this.moving_piece == 0) {
  346.          return false;
  347.       } else {
  348.          this.SetUpdateRegion(xpos, ypos);
  349.          this.moving_piece_xpos = xpos;
  350.          this.moving_piece_ypos = ypos;
  351.          ((Component)this).repaint();
  352.          return true;
  353.       }
  354.    }
  355.  
  356.    public static void main(String[] args) {
  357.       Frame f = new Frame("Checkers game");
  358.       CheckersGame app = new CheckersGame();
  359.       app.init();
  360.       ((Applet)app).start();
  361.       ((Container)f).add("Center", app);
  362.       ((Component)f).setSize(300, 300);
  363.       ((Window)f).show();
  364.    }
  365.  
  366.    public CheckersGame() {
  367.       this.clipWidth = ((Component)this).getSize().width;
  368.       this.clipHeight = ((Component)this).getSize().height;
  369.       this.moving_piece = 0;
  370.       this.moving_piece_x = -1;
  371.       this.moving_piece_y = -1;
  372.    }
  373. }
  374.